package com.northpool.operator.common.json;

import cn.hutool.core.lang.Console;
import com.alibaba.fastjson.JSON;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.operator.utils.StpOperatorHelper;

@OperatorInfo(
        ref = "JsonEscapeOperator",
        name = "Json转义",
        description = "字符串中的特殊字符转义为相应的JSON转义序列"
)
@OutputParams({
        @OutputParam(ref = "execResult", name = "转义结果", type = TypeEnum.STRING)
})
public class JsonEscapeOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "str", name = "字符串", desc = "字符串,如: {\"name\":\"test\"}", required = true)
    })
    public JsonEscapeOperator() {
    }

    public static void main(String[] args) {
        OperatorTestBuild.execute(JsonEscapeOperator.class, TestParamBuilder.aBuilder()
                .param("str", "\"DLBM,tbmj\r\n\t0102,359135.73\r\n\t0201,4680.47\r\n\"\n")
                .build());
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        String str = paramTool.getStr("str");
        String result = JSON.toJSONString(str);
        Console.log("转义前: {}",str);
        Console.log( "转义后: {}", result);
        this.result("execResult", result);
    }

}
